package com.sanery.controller.ajax;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sanery.service.UserService;
import com.sanery.service.impl.UserServiceImpl;

@WebServlet("/reg/checkusername")
public class CheckUserName extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = request.getParameter("username");

		// 查询数据库看用户名是否已存在
		UserService u = new UserServiceImpl();
		Integer count = u.checkUserName(username);
		Boolean bool = true;

		if (count > 0) {
			bool = false;
		}

		// 返回数据
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out = response.getWriter(); // 输出文字
		try {
			out.print("{\"username\":" + bool + "}");
		} finally {
			out.flush();
			out.close();
		}
	}

}
